package com.bocai.yuweiwangluo.activity;


import android.app.AlertDialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.BaseBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.ImagePathInitUtil;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.ShareUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 约战详情界面
 */
public class MatchDetailActivity extends BaseActivity implements View.OnClickListener
{
    @ViewInject(R.id.webview)
    WebView webView;
    @ViewInject(R.id.ll_refresh)
    LinearLayout llRefresh;
    private BaseBean baseBean;
    private ShareUtil shareUtil;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private String url="";
    private String shareUrl="";
    private String shareImagePath="";
    private AlertDialog dialog;
    private String id;
    private String content;
    @Override
    protected int contentView()
    {
        return R.layout.activity_match_detail;
    }

    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        initToolbar("约战详情");
        mToolbar.inflateMenu(R.menu.menu_game_detail);
        id=getIntent().getStringExtra("id");
        content=getIntent().getStringExtra("content");
        url=UrlData.IMAGEURL+"/page/match?id=" + id;
        shareUrl=UrlData.IMAGEURL+"/page/match_share?id="+id+"&s=android";
        setDatas();
        initEvent();
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                shareImagePath= ImagePathInitUtil.initImagePath(MatchDetailActivity.this);
            }
        }).start();
    }

    public void setDatas()
    {
        webView.loadUrl(url);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                if (url.endsWith("add?match_id=" + id))
                {
                    toJoin();
                } else
                {
                    webView.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
            }
        });
    }
    private void toJoin()
    {
        try
        {
            MyOkHttpClient mMyOkHttpClient=new MyOkHttpClient(this,this);
            mMyOkHttpClient.joinMatch(UrlData.BASEURL + UrlData.JOINMATCH, SharePrefencesUtil.getUser_id(this), id, handler);
        }catch (Exception e)
        {

        }
    }

    @Override
    protected void initEvent()
    {
        llRefresh.setOnClickListener(this);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
        {
            @Override
            public boolean onMenuItemClick(MenuItem item)
            {
                View view = LayoutInflater.from(MatchDetailActivity.this).inflate(R.layout.dialog_share, null);
                dialog = new AlertDialog.Builder(MatchDetailActivity.this)
                        .setView(view)
                        .show();
                setDialogView(view);
                return true;
            }
        });
    }
    private void setDialogView(View view)
    {
        shareUtil=new ShareUtil();
        LinearLayout ll_qq=(LinearLayout)view.findViewById(R.id.ll_dialog_qq);
        LinearLayout ll_weixin=(LinearLayout)view.findViewById(R.id.ll_dialog_weixin);
        LinearLayout ll_weixinfriend=(LinearLayout)view.findViewById(R.id.ll_dialog_weixinfriend);
        LinearLayout ll_sinaweibo=(LinearLayout)view.findViewById(R.id.ll_dialog_sinaweibo);
        ll_weixin.setOnClickListener(this);
        ll_weixinfriend.setOnClickListener(this);
        ll_qq.setOnClickListener(this);
        ll_sinaweibo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_refresh:
                llRefresh.setVisibility(View.GONE);
                setDatas();
                break;
            case R.id.ll_dialog_qq:
                shareUtil.showShare(this,dialog,"qq",shareImagePath,
                        "约战",content,shareUrl);
                break;
            case R.id.ll_dialog_weixin:
                shareUtil.showShare(this,dialog,"weixin",shareImagePath,
                        "约战",content,shareUrl);
                break;
            case R.id.ll_dialog_weixinfriend:
                shareUtil.showShare(this,dialog,"weixinfriend",shareImagePath,
                        "约战",content,shareUrl);
                break;
            case R.id.ll_dialog_sinaweibo:
                shareUtil.showShare(this,dialog,"sinaweibo",shareImagePath,
                        "约战",content,shareUrl);
                break;

        }
    }
    private Handler handler=new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            Gson gson = new Gson();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        Log.e("result", result);
                        baseBean=gson.fromJson(result,BaseBean.class);
                        if(0==baseBean.getState())
                        {
                            Toast.makeText(MatchDetailActivity.this,"成功参加",Toast.LENGTH_SHORT).show();

                        }else{
                            Toast.makeText(MatchDetailActivity.this,baseBean.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                        Intent intent=new Intent(MatchDetailActivity.this,ParticipantsActivity.class);
                        intent.putExtra("id", id);
                        startActivity(intent);
                        break;
                    case RESPONSE_FAILED:
                        showError(result);
                        llRefresh.setVisibility(View.VISIBLE);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };
}
